import axios from "axios"
import toast from "../components/toast/index"

const instance = axios.create({
    baseURL: '/api',
    timeout: 5000,
})

instance.interceptors.request.use(
    (config) => {
        const token = localStorage.getItem('x-auth-token')
        if (token) {
            config.headers['x-auth-token'] = token
        }
        return config;
    },
    (err) => Promise.reject(err)
);

instance.interceptors.response.use(
    (response) => {
        // if (response.data !== 200) {
        //     toast({ str: response.data.message, type: 'error' })
        //     return
        // }
        return response.data;
    },
    (err) => {
        toast({ str: '请求失败：' + err.message, type: 'error' })
        Promise.reject(err);
    }
);
export default instance
